<?php

/**
 * This is the model class for table "{{terms}}".
 *
 * The followings are the available columns in table '{{terms}}':
 * @property integer $termID
 * @property integer $categoryID
 * @property string $term_name
 * @property string $created
 * @property string $modified
 */
class Terms extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Terms the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{terms}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('categoryID, term_name,slug', 'required'),
			array(' categoryID', 'numerical', 'integerOnly'=>true),
			array('sortOrder', 'numerical', 'integerOnly'=>true),
			array('term_name', 'length', 'max'=>300),
			array('modified', 'default', 'value' => new CDbExpression('NOW()'),'setOnEmpty' => false, 'on' => 'update'),
         	array('modified,created', 'default', 'value' => new CDbExpression('NOW()'), 'setOnEmpty' => false, 'on' => 'insert'), 
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('termID, categoryID, term_name, created, modified', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'termID' => 'Term',
			'categoryID' => 'Category',
			'term_name' => 'Term Name',
			'created' => 'Created',
			'modified' => 'Modified',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($id = null)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('termID',$this->termID);
		if (!empty($id))
		$criteria->compare('categoryID',$id);
		else
		$criteria->compare('categoryID',$this->categoryID);
		$criteria->compare('term_name',$this->term_name,true);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('modified',$this->modified,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public static function getList($type, $arrParams = array())
	{
		
		
		
	        switch ($type)
	        {
	            case 'PARENT-LIST':
	                return Terms::model()->findAll(array("condition"=>"categoryID=".$arrParams['categoryID']));
	                break;
					

	            case 'ALL':
	            	return Terms::model()->findAll();
	            	break;
				
				case 'ALL-LIST':
	            	return Terms::model()->findAll(array("condition"=>" categoryID='".$arrParams['categoryID']."'"));
	            	break;

	           	case 'ALLVIDEO':
	            	return Terms::model()->findAll(array("condition"=>" categoryID= 8","order"=>"sortOrder"));
	            	break;
	
	        }
	}
	    
	public function getModel($type, $arrParams = array())
    {
        switch ($type)
        {
            case 'TERM-ID':
                return Terms::model()->findByPk($arrParams['termID']);
                break;

            case 'SLUG':
            	$condition = "slug = '".$arrParams['slug']."'";
            	return Terms::model()->find($condition);
            	break;
        }
    }
    public static function getTerm($op)
	{
		$a='';
		if($op != "")
		{
			$arr=explode(',',$op);
			foreach($arr as $key=>$val)
			{   if($key != 0)
				{
				$a.= '<li>'.Terms::model()->getModel("TERM-ID",array("termID"=>$val))->term_name.'</li>';
				}
			}
		}
		return $a;
	}
}